package com.mx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mx.mapper.ArticleMapper;
import com.mx.mapper.ArticleTagMapper;
import com.mx.pojo.Article;
import com.mx.pojo.ArticleTag;
import com.mx.pojo.vo.ArticleVO;
import com.mx.service.IArticleService;
import com.mx.service.IUserService;
import com.mx.utils.MDToText;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MinXu
 * @since 2021-07-28
 * QQ   754647431
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Value("${pageSize}")
    int pageSize;

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    IUserService userService;

    @Autowired
    ArticleTagMapper articleTagMapper;

    public class ArticlePage {
        public List<ArticleVO> articleVOS;
        // 总页数
        public int pages;

        public ArticlePage(List<ArticleVO> articleVOS, int pages) {
            this.articleVOS = articleVOS;
            this.pages = pages;
        }
    }

    public List<ArticleVO> articleToVO(List<Article> articles) {
        ArrayList<ArticleVO> articleVOS = new ArrayList<>();
        for (Article article : articles) {
            String mdToText = MDToText.mdToText(article.getContent());
            if (mdToText.length() >= 221) {
                article.setContent(mdToText.substring(0, 220) + "...");
            }
            articleVOS.add(new ArticleVO(userService.findUserById(article.getUid()), article));
        }
        return articleVOS;
    }

    public ArticlePage findPage(Integer id) {
        //分页
        Page<Article> objectPage = new Page<>(id, pageSize);
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        Page<Article> articlePage = articleMapper.selectPage(objectPage, articleQueryWrapper);
        int pages = (int) articlePage.getPages();
        return new ArticlePage(articleToVO(articlePage.getRecords()), pages);
    }

    public ArticlePage findTagPage(Integer tid, Integer id) {
        List<ArticleTag> articleTags;
        if (tid == -1)
            articleTags = articleTagMapper.selectList(new QueryWrapper<ArticleTag>());
        else
            articleTags = articleTagMapper.selectList(new QueryWrapper<ArticleTag>().eq("tid", tid));
        // 拿到aid列表
        List<Integer> collect = articleTags.stream().map(ArticleTag::getAid).collect(Collectors.toList());
        if (collect.isEmpty()){
            return new ArticlePage(articleToVO(new ArrayList<Article>()), 1);
        }
        //分页
        Page<Article> objectPage = new Page<>(id, pageSize);
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.in("id", collect);
        Page<Article> articlePage = articleMapper.selectPage(objectPage, articleQueryWrapper);
        int pages = (int) articlePage.getPages();
        return new ArticlePage(articleToVO(articlePage.getRecords()), pages);
    }
}
